package com.sxw.hw_student.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxw.hw_student.system.entity.Course;
import com.sxw.hw_student.system.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程表控制器
 */
@RestController
@RequestMapping("/system/sysRole")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    /** 获取所有课程 */
    @GetMapping("/course")
    public List<Course> getCourseSchedule() {
        return courseService.list();
    }

    /** 根据课程编号查询课程 */
    @GetMapping("/course/{courseNo}")
    public Course getCourseById(@PathVariable Integer courseNo) {
        return courseService.getById(courseNo);
    }

    /** 批量保存或更新课程 */
    @PostMapping("/course")
    public boolean saveCourseSchedule(@RequestBody List<Course> courses) {
        boolean isSaved = true;
        for (Course course : courses) {
            isSaved = courseService.saveOrUpdate(course);
        }
        return isSaved;
    }

    /** 单个更新课程 */
    @PutMapping("/course/{courseNo}")
    public boolean updateCourse(@PathVariable Integer courseNo, @RequestBody Course course) {
        // 确保更新的课程编号一致
        course.setCourseNo(courseNo);
        return courseService.updateById(course);
    }
    @GetMapping("/pageRole")
    public Page<Course> pageRole(@RequestParam(required = false) String roleName,
                                  @RequestParam(defaultValue = "1")Integer current,
                                  @RequestParam(defaultValue = "2")Integer size){
        //创建分页对象，将当前页和每页显示的数量传进去（因为mybatiesPlus分页插件只支持Page对象）
            Page<Course> page = new Page<>(current,size);
        //调用service层的分页查询方法,查询角色列表，并将分页对象和查询条件传进去
        Page<Course> sysRolePage = courseService.queryRolePage(page,roleName);

        return sysRolePage;

    }

}

